import {call,put} from 'redux-saga/effects'
import axios from '../utils/request'
import { message} from 'antd';
import { userInfo } from 'os';

export function * getLogin({payload}){//登录
    console.log(payload,'login-payload')
    const res=yield call(axios,{url:'/api/opreaters',method:'post',data:payload})
    console.log(res)
    if(res.code===1){
        window.localStorage.setItem('backToken',res.token);
        // const userinfo=window.btoa(JSON.stringify(res.data))
        // console.log(userinfo,'userinfo')
        window.localStorage.setItem('backUserInfo',JSON.stringify(res.data))
        window.localStorage.setItem('auth',res.data.auth)
        yield put({
            type:'GET_LOGIN',
            payload:res.data
        })
        window.location.href='/layout'
    }
    
}


export function * getOpreaters({payload}){  //获取后台用户
    console.log(payload,'getopreate-payload')
    const res=yield call(axios,{url:'/api/getopreaters',method:'get',params:payload})
    console.log(res)
    if(res.code===1){
        yield put({
            type:'GET_OPREATERS',
            payload:res.data
        })
    }
}

export function * edmitAuth({payload}){  //修改权限
    console.log(payload,'edmitAuth-payload')
    const res=yield call(axios,{url:'/api/edmitauth',method:'put',data:payload})
    console.log(res)
    if(res.code===1){
        message.success(res.data.msg)
        console.log(JSON.parse(window.localStorage.getItem('backUserInfo')).id,'id')
        if(payload.id===JSON.parse(window.localStorage.getItem('backUserInfo')).id){
            window.localStorage.setItem('auth',payload.auth)
        }
        window.location.reload()
    }else{
        message.error('修改失败')
    }
}

export function * delOpreate({payload}){  //删除后台用户
    console.log(payload)
    const res=yield call(axios,{url:`/api/delopreate/${payload}`,method:'delete'})
    console.log(res)
    if(res.code===1){
        message.success(res.data.msg);
    }else{
        message.error(res.data.msg);
    }

}

